//
//  BindOilcardViewController.m
//  EasyToTesco
//
//  Created by mc on 16/3/2.
//  Copyright © 2016年 Jack. All rights reserved.
//

#import "BindOilcardViewController.h"
#import "ReadLoginData.h"
#import "AFNetworking.h"
#define WIDTH [[UIScreen mainScreen] bounds].size.width
#define NORMWIDTH [[UIScreen mainScreen] bounds].size.width / 375
#define HEIGHT [[UIScreen mainScreen] bounds].size.height
#define NORMHEIGHT [[UIScreen mainScreen] bounds].size.height / 667
#define FRAME [[UIScreen mainScreen] bounds]
@interface BindOilcardViewController ()<UIAlertViewDelegate>

{
    NSInteger orderstate;
    BOOL BtnState;
    NSString *usernameStr;
}

@property (weak, nonatomic) IBOutlet UITextField *yongHuMing;

@property (weak, nonatomic) IBOutlet UITextField *zhiFuTextField;

@property (weak, nonatomic) IBOutlet UITextField *youKaTextField;


@end

@implementation BindOilcardViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self addMyOrderTopView];
   
    [self getdata];
   
}













// 确定
- (IBAction)changeButton:(id)sender {
    
    AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
    mgr.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    NSDictionary *dic= @{@"username":usernameStr,
                         @"userpwd":self.payWord.text,
                         @"youka":self.youKa.text
                         };
    NSLog(@"%@", usernameStr);
    [mgr GET:@"http://www.ylgw8.com/AppUser/setUserYouka" parameters:dic success:^(NSURLSessionDataTask *task, id responseObject) {
        NSDictionary *dict = [self toArrayOrNSDictionary:responseObject];
        NSLog(@"%@, %@", dict, dict[@"message"]);
        if ([dict[@"messange"] isEqualToString:@"绑定成功"]) {
            UIAlertView *alview = [[UIAlertView alloc] initWithTitle:@"提示" message:@"绑定成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            alview.tag = 123304;
            [alview show];
            
            
        }else{
            
            UIAlertView *alview = [[UIAlertView alloc] initWithTitle:@"提示" message:@"绑定失败" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            alview.tag = 123305;
            [alview show];
            
        }
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"失败");
    }];
    
}
- (void)getdata {
    // 获取当前账号
    NSDictionary *dic = [ReadLoginData readLoginData];
    if (dic.allKeys.count == 0) {
        UIAlertView *alview = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您尚未登录,请登录后再次操作" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        alview.tag = 123303;
        [alview show];
        
    }else {
        
        usernameStr = [dic[@"data"] objectForKey:@"username"];
          self.userName.text = usernameStr;
    }
    
}


- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    
    if (alertView.tag == 123303) {
          [self dismissViewControllerAnimated:YES completion:nil];
    }else{
        
    }
    
  
    
    
    
}
- (void)addMyOrderTopView {
    
    UIImageView *image1 = [[UIImageView alloc] initWithFrame:CGRectMake(10*NORMWIDTH, (self.secondView.frame.size.height-20)/2, 20*NORMWIDTH, 20*NORMHEIGHT)];
    image1.image = [UIImage imageNamed:@"goback-icon.png"];
    [self.secondView addSubview:image1];
    self.leftBtn.hidden = YES;
    
    UIButton *goBackBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    goBackBtn.frame = CGRectMake(0, 0, self.secondView.frame.size.height, self.secondView.frame.size.height);
    [goBackBtn addTarget:self action:@selector(gobackClick) forControlEvents:UIControlEventTouchUpInside];
    [self.secondView addSubview:goBackBtn];
    self.titleLabel.text = @"绑定油卡";
    
}

- (void)gobackClick {
    [self dismissViewControllerAnimated:YES completion:nil];
}



- (id)toArrayOrNSDictionary:(NSData *)jsonData{
    NSError *error = nil;
    id jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:&error];
    if (jsonObject != nil && error == nil){
        return jsonObject;
    }else{
        // 解析错误
        return nil;
    }
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.yongHuMing resignFirstResponder];
    [self.zhiFuTextField resignFirstResponder];
    [self.youKa resignFirstResponder];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
